package edu.jinghuan.lms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import edu.jinghuan.lms.entity.OverdueConfig;
import edu.jinghuan.lms.mapper.OverdueConfigMapper;
import edu.jinghuan.lms.service.OverdueConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class OverdueConfigServiceImpl implements OverdueConfigService {

    @Autowired
    private OverdueConfigMapper overdueConfigMapper;

    @Override
    public OverdueConfig setOverdueDays(Integer days) {
        if (days == null || days <= 0) {
            throw new IllegalArgumentException("超期天数必须大于0");
        }

        // 获取现有配置或创建新配置
        OverdueConfig config = getCurrentConfig();
        if (config == null) {
            config = new OverdueConfig();
        }
        
        config.setDays(days);
        
        if (config.getId() == null) {
            overdueConfigMapper.insert(config);
        } else {
            overdueConfigMapper.updateById(config);
        }
        
        return config;
    }

    @Override
    public OverdueConfig getCurrentConfig() {
        return overdueConfigMapper.selectOne(new LambdaQueryWrapper<>());
    }
}
